# -*- coding: utf-8 -*-
# -----------------------------------------
# @Time       : 2020/12/28 9:17
# @Author     : dqz 
# @Email      : 1406990390@qq.com
# @File       : translate_txt_to_html
# @Software   : PyCharm
# -----------------------------------------
import os
import sys

base_dir = os.path.dirname(os.path.abspath(__file__))

dir_output = os.path.join(base_dir, "output_html")
path_output = os.path.join(base_dir, 'output')

if sys.platform.startswith("win"):
    dir_output = dir_output.replace("/", "\\")
    path_output = path_output.replace("/", "\\")


def get_txt_file():
    files = os.listdir(path_output)
    return [file for file in files if file.endswith(".txt")]


def translate_to_html(txt):
    name = txt.split(".")[0]
    output_name = f"{dir_output}/{name}.html"
    input_file = os.path.join(path_output, txt)
    if sys.platform.startswith("win"):
        output_name = output_name.replace("/", "\\")
        input_file = input_file.replace("/","\\")

    with open(output_name, 'w',encoding="utf-8") as fw:
        fw.write("""<header>\n
                    <title>回退</title>\n
                    <style>\n
                        .update{ background-color:#556B2F}\n
                        .delete{ background-color:#006400}\n
                        .insert{ background-color:#483D8B}\n
                        .comment{ background-color:#F4A460}\n
                        .step{ background-color:#3300CC}\n
                        div {\n
                            color:#FFFFFF;\n 
                            margin:3;\n
                            padding:3;\n
                        }\n
                    </style>\n
                    <header>\n
                    <body>\n""")
        for line in open(input_file,"r",encoding="utf-8"):
            if line.startswith("UPDATE"):
                fw.write(f"<div class='update'>{line}</div>\n")
            elif line.startswith("DELETE"):
                fw.write(f"<div class='delete'>{line}</div>\n")
            elif line.startswith("INSERT"):
                fw.write(f"<div class='insert'>{line}</div>\n")
            elif line.startswith("#"):
                if line.startswith("####"):
                    fw.write(f"<div class='step'>{line}</div>\n")
                else:
                    fw.write(f"<div class='comment'>{line}</div>\n")
        else:
            fw.write("</body>")


def start_translate():
    files = get_txt_file()
    for file in files:
        translate_to_html(file)

if __name__=="__main__":
    start_translate()